﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('cost.orderNo')+' :'">
          <el-input
            v-model="listQuery.order_no"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('cost.orderNo')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('rule.name')+' :'">
          <el-input
            v-model="listQuery.rule_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('rule.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.adMaster')+' :'">
          <el-input
            v-model="listQuery.ad_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.adMaster')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('video.name')+' :'">
          <el-input
            v-model="listQuery.video_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('video.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('cost.type')+' :'">
          <el-select
            v-model="listQuery.type"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('cost.type')"
          >
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('video.uploadTime')+' :'">
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.startPayTime"
              :picker-options="pickerOptionsB"
              type="date"
              :placeholder="$t('video.startUploadTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.endPayTime"
              :picker-options="pickerOptionsE"
              type="date"
              :placeholder="$t('video.endUploadTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="headAdd">
      <el-button
        v-for="item in sortList"
        :key="item.id"
        style="margin-left: 10px"
        class="filter-item"
        @click="locationFilter(item.id)"
      >{{ item.name }}</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('cost.orderNo')">
        <template slot-scope="scope">
          <span>{{ scope.row.order_no }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('rule.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.rule_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('video.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.video_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.adMaster')">
        <template slot-scope="scope">
          <span>{{ scope.row.ad_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('cost.type')">
        <template slot-scope="scope">
          <span>{{ costTypeES(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('cost.adCurrency')">
        <template slot-scope="scope">
          <span>{{ scope.row.ad_currency }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('cost.extraCurrency')">
        <template slot-scope="scope">
          <span>{{ scope.row.extra_currency }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('cost.payTime')">
        <template slot-scope="scope">
          <span>{{ timeToData(scope.row.pay_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Location",
  data() {
    return {
      showSearchItem: false,
      loading: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      typeOptions: [
        {
          value: 1,
          label: "视频播放"
        },
        {
          value: 2,
          label: "广播"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        order_no: "",
        rule_name: "",
        ad_name: "",
        video_name: "",
        type: "",
        time_type: "",
        startPayTime: "",
        endPayTime: ""
      },
      dialogStatus: "",
      textMap: {
        update: "更新",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      sortList: [
        { id: 1, name: "昨天" },
        { id: 2, name: "7天内" },
        { id: 3, name: "一个月内" }
      ],
      pageSize: 20,
      permission: {
        add: false,
        del: false,
        review: false
      },
      pickerOptionsB: {
        // 限制时间
        // selectableRange: ['22:00:00 - 23:59:59','00:00:00 - 02:00:00'],
        // 限制日期
        disabledDate: this.disabledDateB
      },
      pickerOptionsE: {
        disabledDate: this.disabledDateE
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.del) {
        itemCount += 1;
      }
      if (this.permission.update) {
        itemCount += 1;
      }
      if (this.permission.review) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.add = true;
      this.permission.review = true;
      this.permission.addSort = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 219) {
          this.permission.add = true;
        } else if (id === 220) {
          this.permission.review = true;
        } else if (id === 222) {
          this.permission.addSort = true;
        }
      });
    }
    this.getList();
  },
  methods: {
    disabledDateB(time) {
      if (this.listQuery.endPayTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endPayTime).getTime() +
            24 * 3600 * 1000 -
            8.64e7
        );
      }
    },
    disabledDateE(time) {
      if (this.listQuery.startPayTime) {
        return (
          time.getTime() <
          new Date(this.listQuery.startPayTime).getTime() - 8.64e6
        );
      }
    },
    locationFilter(location) {
      this.listQuery.page = 1;
      this.listQuery.time_type = location;
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        order_no: "",
        rule_name: "",
        ad_name: "",
        video_name: "",
        type: "",
        time_type: "",
        startPayTime: "",
        endPayTime: ""
      };
      this.getList();
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.$router.push({
        name: "adPlayLog/index",
        params: {
          rule_id: row.rule_id,
          startPlayTime: row.pay_time
        }
      });
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.order_no) {
        data["order_no"] = this.listQuery.order_no;
      }
      if (this.listQuery.rule_name) {
        data["rule_name"] = this.listQuery.rule_name;
      }
      if (this.listQuery.ad_name) {
        data["ad_name"] = this.listQuery.ad_name;
      }
      if (this.listQuery.video_name) {
        data["video_name"] = this.listQuery.video_name;
      }
      if (this.listQuery.type) {
        data["type"] = this.listQuery.type;
      }
      if (this.listQuery.time_type) {
        data["time_type"] = this.listQuery.time_type;
      }
      if (this.listQuery.startPayTime) {
        data["startPayTime"] = this.listQuery.startPayTime;
      }
      if (this.listQuery.endPayTime) {
        const date = new Date(this.listQuery.endPayTime);
        data["endPayTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }

      const merchantCircleIndexPath = {
        // 获取付费列表
        company: "STQ",
        api: "pc_cost_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(merchantCircleIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_cost_index") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          }
        }
      }
    },
    timeToData(time) {
      const date = new Date(time);
      return this.timestampToTime(date.getTime(), "{y}-{m}-{d}");
    }
  }
};
</script>
